﻿Imports Crystal.CLL
Imports CrystalDataHub.BLL
Imports Infragistics.Win.UltraWinGrid
Imports System.Text

Public Class ListSnapshot
    Inherits BaseForm

#Region "Fields"
    Private mclsImporteSourceManager As New ImporteSourceManager
    Public mintProjectID As Integer
    Public mintListingID As Integer
    Public mintSnapshotEntityID As Integer
    Public mblnIsCompleted As Boolean

    Public mintSnapshotTemplateID As Int16
    Public mstrSnapshotSourceName As String
    Public mintSnapshotSourceTypeID As Int16
    Public mstrSnapshotFilePath As String
    Public mstrSourceTypeCode As String
    Public mstrDataSourceName As String

    Public mintRegularRadio As Int16
    Public mblnRetail As Boolean
    Public mblnCustodian As Boolean
    Public mblnCreateNew As Boolean
    Public mintAdvancedRadio As Int16
    Public mintRowNumber As Integer
    Public mstrHiddenColumns As String
    Private mstrScreenName As String = "List Snapshot"
    Private mdtDeletedRows As DataTable

#End Region

#Region "On Load"

    Private Sub ListSnapshot_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CallOnLoad()
    End Sub

    Private Sub CallOnLoad()
        SetOnLoadControls()
        LoadGridData()
    End Sub

    Public Sub SetOnLoadControls()
        Me.Text = TitleManager.GetTitle("ListSnapshot")
    End Sub

#End Region

#Region "Events"

#Region "Form Events"
    Private Sub ListSnapshot_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub ListSnapshot_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        DeleteRecords()
    End Sub
#End Region

#Region "Grid Events"

    Private Sub grdSnapshotList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdSnapshotList.Click
        Dim index As Integer = 0
        Dim gridRow As UltraGridRow
        Dim drv As DataRowView
        Dim row As DataRow
        Dim lintEntityID As String
        If grdSnapshotList.ActiveCell Is Nothing Then Exit Sub
        If grdSnapshotList.ActiveCell.Column.Header.Caption = "Delete" Then
            If (MessageBox.Show("Do you want to delete?", CommonCLL.mstrMsgBoxCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No) Then
                grdSnapshotList.ActiveCell = Nothing
                Return
            End If
            lintEntityID = grdSnapshotList.ActiveRow.Cells("EntityID").Value.ToString
            DeleteSnapshot(grdSnapshotList.ActiveRow.Cells("FilePath").Value.ToString)
            If mdtDeletedRows Is Nothing Then
                mdtDeletedRows = CType(grdSnapshotList.DataSource, DataTable).Clone()
            End If
            gridRow = grdSnapshotList.ActiveRow
            gridRow.Hidden = True
            drv = TryCast(gridRow.ListObject, DataRowView)
            row = If(drv IsNot Nothing, drv.Row, Nothing)
            mdtDeletedRows.ImportRow(row)
            ShowMessageBox(DBResult.RECORDS_DELETED)
            If mdtDeletedRows IsNot Nothing Then
                If mdtDeletedRows.Rows.Count = grdSnapshotList.Rows.Count Then
                    Me.Close()
                End If
            End If
            For Each drow As UltraGridRow In grdSnapshotList.Rows
                If drow.Hidden = False Then
                    drow.Activate() ' = True
                    Exit Sub
                End If
            Next
        End If
    End Sub

    Private Sub DeleteSnapshot(ByVal strFilePath As String)
        If System.IO.File.Exists(strFilePath) = True Then
            System.IO.File.Delete(strFilePath)
        End If
    End Sub

    Private Sub grdSnapshotList_DoubleClickRow(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs) Handles grdSnapshotList.DoubleClickRow
        OpenMappings()
    End Sub

#End Region

#Region "Button Events"

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        OpenMappings()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

#End Region

#End Region

#Region "Procedures"

    ''' <summary>
    ''' To load the list of incompleted snapshots in the grid
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadGridData()
        Dim ldtSnapshots As New DataTable
        Dim ldrSnapshots As IDataReader
        Try
            ldrSnapshots = mclsImporteSourceManager.GetSnapshots(mintListingID, mintSnapshotEntityID, mblnIsCompleted)
            ldtSnapshots.Load(ldrSnapshots)
            CommonCLL.ChangeDataReaderDataTable(ldtSnapshots)
            If Not ldtSnapshots.Columns.Contains("Delete") Then
                ldtSnapshots.Columns.Add("Delete", GetType(Bitmap))
            End If
            grdSnapshotList.DataSource = ldtSnapshots
            'Show the Snapshots in Tree Structure SourceType wise
            grdSnapshotList.Rows.Band.Columns("EntityID").Hidden = True
            grdSnapshotList.Rows.Band.Columns("ETemplateID").Hidden = True
            grdSnapshotList.Rows.Band.Columns("SourceTypeID").Hidden = True
            grdSnapshotList.Rows.Band.Columns("FilePath").Hidden = True
            grdSnapshotList.Rows.Band.Columns("SourceTypeCode").Hidden = True
            grdSnapshotList.Rows.Band.Columns("RegularRadio").Hidden = True
            grdSnapshotList.Rows.Band.Columns("Retail").Hidden = True
            grdSnapshotList.Rows.Band.Columns("Custodian").Hidden = True
            grdSnapshotList.Rows.Band.Columns("CreateNew").Hidden = True
            grdSnapshotList.Rows.Band.Columns("AdvancedRadio").Hidden = True
            grdSnapshotList.Rows.Band.Columns("RowNumber").Hidden = True
            grdSnapshotList.Rows.Band.Columns("DataSourceName").Hidden = True
            grdSnapshotList.Rows.Band.Columns("HiddenColumns").Hidden = True
            grdSnapshotList.DisplayLayout.Override.AllowColSizing = AllowColSizing.Free
            grdSnapshotList.Rows.Band.Columns("Column Title").PerformAutoResize(PerformAutoSizeType.VisibleRows)

        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            ldtSnapshots = Nothing
            ldrSnapshots = Nothing
        End Try
    End Sub

    Protected Function CreateListingXML(ByVal ldtEntityID As DataTable) As StringBuilder
        Dim i As Integer
        Dim Logmessage As New System.Text.StringBuilder
        Dim sbXML As New StringBuilder
        'sbXML.Append("<?xml version=" & """1.0" & """ encoding=" & """Windows-1252" & """?>")
        Try
            If ldtEntityID.Rows.Count > 0 Then
                sbXML.Append("<Root>")
                For i = 0 To ldtEntityID.Rows.Count - 1
                    If Not CommonCLL.blnRowBlank(ldtEntityID.Rows(i)) Then
                        sbXML.Append("<Row EntityID=""" & ldtEntityID.Rows(i)("EntityID").ToString().Replace("'", "&apos;").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(ControlChars.Quote, "&quot;") & """ " & _
                                "/>")
                        sbXML.Append(vbNewLine)
                    End If
                Next
                sbXML.Append("</Root>")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Logmessage.Append("There is error in grdClientDetail_AfterCellListCloseUp of form findmanagement.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
        End Try
        Return sbXML
    End Function

    Private Sub DeleteRecords()
        Dim dataString As String
        If mdtDeletedRows IsNot Nothing Then
            dataString = CreateListingXML(mdtDeletedRows).ToString
            mclsImporteSourceManager.DeleteRecord(dataString)
        End If
    End Sub

    Private Sub OpenMappings()
        If grdSnapshotList.ActiveRow Is Nothing OrElse grdSnapshotList.ActiveRow.Cells Is Nothing Then
            Me.ShowMessageBox(DBResult.EDS_SELCT_SNPSHOT)
            Exit Sub
        End If
        'Store the saved snapshot settings' in variables
        mstrSnapshotSourceName = grdSnapshotList.ActiveRow.Cells("Column Title").Value
        mintSnapshotSourceTypeID = grdSnapshotList.ActiveRow.Cells("SourceTypeID").Value
        mintSnapshotTemplateID = grdSnapshotList.ActiveRow.Cells("ETemplateID").Value
        mintSnapshotEntityID = grdSnapshotList.ActiveRow.Cells("EntityID").Value
        mstrSnapshotFilePath = grdSnapshotList.ActiveRow.Cells("FilePath").Value
        mstrSourceTypeCode = grdSnapshotList.ActiveRow.Cells("SourceTypeCode").Value
        mstrDataSourceName = grdSnapshotList.ActiveRow.Cells("DataSourceName").Value

        mintRegularRadio = grdSnapshotList.ActiveRow.Cells("RegularRadio").Value
        mblnRetail = grdSnapshotList.ActiveRow.Cells("Retail").Value
        mblnCustodian = grdSnapshotList.ActiveRow.Cells("Custodian").Value
        mblnCreateNew = grdSnapshotList.ActiveRow.Cells("CreateNew").Value
        mintAdvancedRadio = grdSnapshotList.ActiveRow.Cells("AdvancedRadio").Value
        mintRowNumber = grdSnapshotList.ActiveRow.Cells("RowNumber").Value
        mstrHiddenColumns = grdSnapshotList.ActiveRow.Cells("HiddenColumns").Value
        Me.Close()
    End Sub

#End Region

    Private Sub grdSnapshotList_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdSnapshotList.InitializeLayout
        e.Layout.Bands(0).Columns("Delete").CellAppearance.ImageHAlign = Infragistics.Win.HAlign.Center
    End Sub

    Private Sub grdSnapshotList_InitializeRow(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeRowEventArgs) Handles grdSnapshotList.InitializeRow
        e.Row.Cells("Delete").Value = Crystal.CommonDBManagement.My.Resources.delete_ico
    End Sub
End Class